#include "includes.h"

unsigned char calculate=0;
char buffer[15];
volatile unsigned int current_time,previous_time,time;
unsigned int T;
volatile unsigned char input_timer;
volatile unsigned char counter_pwm;

/*****************************************************************************
Name:           Delay
Description:    Delay program for ...ms
Parameters:     ms
Returns:        none
*****************************************************************************/

void Delay(int ms)
{
  int i;

  for (i=0;i<ms;i++)
      _delay_ms(1);
}

/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/

void buzzer_init(void)
{
	DDRB|=(1<<4);
}


/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/
void buzzer(unsigned int repeat)
{
    unsigned char i=0;
	while(i<repeat)
	{
	BUZZER_ON;
	_delay_ms(1);
	BUZZER_OFF;
	_delay_ms(1);
	i++;
	}
}



/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/

void temp2lcd(void)
{

	/*lcd_gotoxy(0,0);
	adc_init(0x00);
    adc_lcd();  
	lcd_puts(" ");
	read_temperature();	
	Delay(1000);*/
}

void temp2uart(void)
{

	//USART_Transmit(0x55);
}

void counter2lcd(void)
{
	char buffer[4];
	itoa(counter_pwm,buffer,10);
	lcd_gotoxy(0,1);
	lcd_puts(buffer);
}

/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/
ISR (INT2_vect) 
{

    SREG = 0;
	pwm_setduty(255);
	lcd_clrscr();
	lcd_puts("PWM 255");
    buzzer(50);
	//USART_Transmit('2');
	Delay(400);
	/*
	if(counter_pwm==0) pwm_init();
	if(counter_pwm<245)
	{
		counter_pwm+=10;
		counter2lcd();
    	pwm_setduty(counter_pwm);
		
	}*/
    SREG |= 0x80;
}
ISR (INT1_vect) 
{
    SREG = 0;
	pwm_setduty(10);
	lcd_clrscr();
	lcd_puts("PWM 10");
    buzzer(50);
//	USART_Transmit('1');
	Delay(400);
	/*
	if(counter_pwm>10)
	{
		counter_pwm-=10;
		counter2lcd();
		pwm_setduty(counter_pwm);
		
	}*/
	
    SREG |= 0x80;
}
ISR (INT0_vect) 
{
    SREG = 0;
	lcd_clrscr();
	lcd_puts("PWM stopped!");
	pwm_setduty(0);
	pwm_off();
	BUZZER_OFF;
    Delay(300);
	SREG |= 0x80;
}

ISR (TIMER1_CAPT_vect)
{
	SREG =0;
	
	static volatile int timeAtLastEdge;
    uint16_t lastSample = timeAtLastEdge;
    timeAtLastEdge = ICR1;
    time = timeAtLastEdge - lastSample; 
	SREG |= 0X80;
}
void disp_time(void)
{

			/*lcd_putc('0'+current_time/1000);
			current_time/=1000;
			lcd_putc('0'+current_time/100);
			lcd_putc('0'+current_time/10);
			lcd_putc('0'+current_time%10);*/
			sprintf(buffer,"%d",time);
			lcd_putc('#');
			lcd_puts(buffer);
}

void interrupt_init(void){
  
    GICR = 0;
    GICR &= 0xDF;
    GIFR |= (1<<INTF2)|(1<<INTF1)|(1<<INTF0);
    GICR |= (1<<INT2)|(1<<INT1)|(1<<INT0);
	TIMSK |= (1<<TICIE1);
    SREG |= 0x80;

}